<?php

$PATH = 'bonsai/common';
require_once "$PATH/file/File.php";
require_once "$PATH/file/Image.php";
require_once "$PATH/util/Collection.php";

$PATH = 'cpanel';
require_once "$PATH/model/entity/Language.php";
require_once "$PATH/model/entity/Album.php";
require_once "$PATH/model/dao/LanguageDAO.php";
require_once "$PATH/model/dao/AlbumDAO.php";
require_once "$PATH/controller/CPanelController.php";

require_once "$PATH/model/entity/Album.php";
require_once "$PATH/model/entity/Picture.php";
require_once "$PATH/model/entity/LanguageDetail.php";
require_once "$PATH/model/entity/AlbumDetail.php";
require_once "$PATH/model/dao/AlbumDAO.php";
require_once "$PATH/model/dao/AlbumDetailDAO.php";
require_once "$PATH/model/dao/PictureDAO.php";
require_once "$PATH/business/AlbumBusiness.php";

/**
 * Enter description here ...
 * @author NgoAnhTu
 * @date 2013-10-16
 */
final class AlbumController extends CPanelController {
	
	const VIEW_PATH = 'cpanel/view/album/';
	const NAME = 'album';
	const PICS_ITEMS = 5;
	
	/**
	 * @var AlbumBusiness
	 */
	private $albumBusiness;
	
	/**
	 * Constructor
	 */
	public function AlbumController () {
		parent::CPanelController();
		$this->albumBusiness = AlbumBusiness::getInstance();
	}
	
	/**
	 * (non-PHPdoc)
	 * @see SecurityController::checkSecurity()
	 */
	protected function checkSecurity() {
// 		if (!isset($_SESSION[Dispatcher::USER_SESSION])) {
// 			$this->redirect('user', 'login');
// 		}
	}

	/**
	 * Action: View all Albums
	 * @action(grid)
	 */
	public function grid($ids, $page = 1, $pattern = '') {
		$MSG_CONFIRM = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_DEL_CONFIRM);
		if (!HTTP::isGet()) {
			$this->albumBusiness->active($ids);
			$this->msg(self::NAME, 'grid', '', str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_UPDATE_STATUS), 2);
		} 
		$albums = $this->albumBusiness->findAll($page, $pattern);
		require_once self::VIEW_PATH . 'grid.html';
	}
	
	/**
	 * Action: Create new Langauge
	 * @action(create)
	 */
	public function create($name, $img, $names, $codes) {
		$msg = '';
		
		if (!HTTP::isGet()) {
			$name = trim($name);
			
			if (!$this->albumBusiness->isExist($name)) {
				$album = $this->albumBusiness->create($name, $img, $names, $codes);
				if ($album != null) {
					$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
					$this->msg(self::NAME, 'grid', '', $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				}
			} else {
				$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, $name, $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'create.html';
	}

	/**
	 * Action: Edit Album
	 * @action(edit) 
	 */
	public function edit($id, $name, $img, $names, $codes) {
		$msg = '';
		
		$album = $this->albumBusiness->findById($id);
		if ($album == null) {
			$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_NOT_EXIST);
			$msg = str_replace(LG::VAL, $id, $msg);
			$this->msg(self::NAME, 'grid', '', $msg);
		}
		
		if (!HTTP::isGet()) {
			$name = trim($name);
			
			if (!$this->albumBusiness->isExistOther($id, $name)) {
				$album->name = $name;
				$album = $this->albumBusiness->update($album, $img, $names, $codes);
				if ($album != null) {
					$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				}
				$this->msg(self::NAME, 'grid', '', $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, "$id : $name", $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'edit.html';
	}
	
	/**
	 * Action: Delete Album
	 * @action(del) 
	 */
	public function del($id) {
		$msg = '';
		$album = $this->albumBusiness->findById($id);
	
		if ($album == null) {
			$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_NOT_EXIST);
			$msg = str_replace(LG::VAL, $id, $msg);
		} else {
			if ($this->albumBusiness->del($id) != null) {
				$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_DELETED);
				$msg = str_replace(LG::VAL, $album->name, $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::ALBUM, LG::MSG_NOT_DEL);
				$msg = str_replace(LG::VAL, $album->name, $msg);
			}
		}
		
		$this->msg(self::NAME, 'grid', '', $msg);
	}
	
	/**
	 * Action: View list of Image
	 * @action(pics) 
	 */
	public function pics($id, $picIDs, $delIDs, $orders, $uploadOrders, $imgs, $behavior) {
		$MSG_CONFIRM = str_replace(LG::ENTITY, LG::IMAGE, LG::MSG_DEL_CONFIRM);
		$album = $this->albumBusiness->findById($id);
		$pics = $this->albumBusiness->getPics($id); 
		if (!HTTP::isGet()) {
			if ($behavior == 'update') {
				if ($this->albumBusiness->updatePicOrders($picIDs, $orders)) {
					$msg = str_replace(LG::ENTITY, LG::IMAGES, LG::MSG_UPDATE_STATUS);
				} else {
					$msg = str_replace(LG::ENTITY, LG::IMAGE, LG::MSG_NOT_UPDATE_STATUS);
				}
			} else if ($behavior == 'delete') {
				if ($this->albumBusiness->deletePics($delIDs)) {
					$msg = str_replace(LG::ENTITY, LG::IMAGES, LG::MSG_DELETED);
				} else {
					$msg = str_replace(LG::ENTITY, LG::IMAGE, LG::MSG_NOT_DEL);
				}
				$msg = str_replace(LG::VAL, '...', $msg);		
			} else if ($behavior == 'upload') {
				$count = $this->albumBusiness->insertPics($id, $uploadOrders, $imgs);
				if ($count > 0) {
					$msg = str_replace(LG::ENTITY, LG::IMAGE, LG::MSG_ADD);
					$msg = str_replace(LG::VAL, $count, $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::IMAGE, LG::MSG_NOT_ADD);					
				}
			}
			$this->msg(self::NAME, 'pics', "id=$id", $msg, 1);
		}
		require_once self::VIEW_PATH . 'pics.html';
	} 
	
}
?>